<template>
  <div class="app-container">
    <div class="div-sch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="isDelete">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="启用" value="true"></el-option>
            <el-option label="禁用" value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="div-t">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" border :data="postList"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="员工姓名" align="center" prop="staffName" />
        <el-table-column label="所属公司" align="center" prop="deptName" />
        <el-table-column label="手机号" align="center" prop="phone" />
        <el-table-column label="状态" align="center" prop="isDelete">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"       
              v-model="!scope.row.isDelete" 
              active-text="ON"          
              inactive-text="OFF"  
              @change="changeStart(scope.row)"     
              active-color="#2d8cf0"    
              inactive-color="#ccc">  
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >修改</el-button>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']"
            >删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <!-- 添加或修改的对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="员工姓名" prop="staffName">
            <el-input v-model="form.staffName" placeholder="请输入员工姓名" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="所属公司" prop="deptId">
            <el-select v-model="form.deptId" placeholder="请选择所属公司" clearable
              :style="{width: '100%'}"  collapse-tags>
              <el-option v-for="(item, index) in deptOptions" :key="index" :label="item.deptName" 
                :value="item.deptId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="启用状态" prop="phone">
            <el-radio-group v-model="form.isDelete" size="medium" >
                <el-radio :label="false" >启用</el-radio>
                <el-radio :label="true" >禁用</el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="id" v-show>
            <el-input v-model="form.id" ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {whiteApi} from "@/api/system/white";

export default {
  name: "white",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: undefined,
        status: undefined
      },
      deptOptions: [{
        "deptName": "选项一",
        "deptId": 1
      }, {
        "deptName": "选项二",
        "deptId": 2
      }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /^1(3|4|5|7|8|9)\d{9}$/,
          message: '手机号格式错误',
          trigger: 'blur'
        }],
        isDelete: [
          { required: true, message: "请选择启用状态", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "请选择所属公司", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTableHeight();
    this.createDeptOptions();
  },
  methods: {
    getTableHeight(){
      this.tableHeight = window.innerHeight - 300;
    },
    createDeptOptions(){
      whiteApi.getDeptOptions().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询 */
    getList() {
      this.loading = true;
      whiteApi.getWhiteList(this.queryParams).then(response => {
        this.postList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      }).catch(err=>{
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        phone: undefined,
        isDelete: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection)
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
      this.form.isUpdate = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改标签";
      this.form = this.deepClone(row);
      this.form.isUpdate = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.isUpdate) {
            whiteApi.updateWhite(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            whiteApi.addWhite(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
       this.$modal.confirm('是否确认删除？',"提示").then(res=>{
        whiteApi.batchDeleteWhite({
          ids:this.ids
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      })
    },
    chooseDept(row){
      console.log(row)
      alert(123)
    },
    changeStart(row){
      whiteApi.changeStart({isDelete:row.isDelete,id:row.id})
        .then(res=>{
          this.$message.success('执行成功!');
          this.getList();
        })
        .catch(error=>{
          console.error(error);
        })
    },
  }
};
</script>
<style scoped>
.div-t {
  background: white;
  padding: 10px;
  border-radius: 5px;
  /* border: 1px solid rgb(211, 208, 208); */
}
.div-sch {
  background: white;
  padding: 10px 10px 0px 10px;
  border-radius: 5px;
  margin-bottom: 10px;
}
.switchStyle .el-switch__label {
    position: absolute;
    display: none;
    color: #fff;
  }
 .el-switch__core{
    background-color: rgba(166, 166, 166, 1) ;
  }
  .switchStyle .el-switch__label--left {
    z-index: 9;
    left: 8px;
  }
  .switchStyle .el-switch__label--right {
    z-index: 9;
    left: -15px;
  }
  .switchStyle .el-switch__label.is-active {
    display: block;
  }
  .switchStyle.el-switch .el-switch__core,
  .el-switch .el-switch__label {
    width: 60px !important;
  }
</style>
